﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ReplenishmentBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInventoryMgtDA inventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly IInvLotDA invlotDA = DataAccess.CreateInvLotDA();
        private static readonly IInvLotLocLpnDA invLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
        private static readonly IInvSkuLocDA invskuloc = DataAccess.CreateInvSkuLocDA();
        private static readonly IInvSkuLocDA InvSkuLoc = DataAccess.CreateInvSkuLocDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReplenishmentDA replenishmentDA = DataAccess.CreateReplenishmentDA();
        private static readonly IRotationRuleDA rotataionRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotataionRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly ITaskDetailDA taskDetailDA = DataAccess.CreateTaskDetailDA();

        public ReplenishmentBLL()
        {
            this.pubParasInfo = null;
        }

        public ReplenishmentBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ReplenishmentBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteReplenishment(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<ReplenishmentInfo> ReplenishmentInfos = new List<ReplenishmentInfo>();
                ReplenishmentInfo replenishmentInfo = null;
                foreach (string id in IDS)
                {
                    replenishmentInfo = replenishmentDA.GetReplenishmentByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    ReplenishmentInfos.Add(replenishmentInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (replenishmentDA.DeleteReplenishment(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<ReplenishmentInfo>(dataBase, tran, ReplenishmentInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "REPLENISHMENT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public List<TaskDetailInfo> GenerateReplenishment(ReplenishmentGenParamsEntity replenishmentGenParamsEntity)
        {
            string whLoginID = this.dataBaseID[0];
            List<TaskDetailInfo> taskDetailList = new List<TaskDetailInfo>();
            List<string> sqlWhere = new List<string>();
            if (!(string.IsNullOrEmpty(replenishmentGenParamsEntity.StartOwnerID) || string.IsNullOrEmpty(replenishmentGenParamsEntity.EndOwnerID)))
            {
                sqlWhere.Add(" ISL.OWNER_ID   BETWEEN '" + replenishmentGenParamsEntity.StartOwnerID + "' AND '" + replenishmentGenParamsEntity.EndOwnerID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.StartOwnerID))
            {
                sqlWhere.Add(" ISL.OWNER_ID   >= '" + replenishmentGenParamsEntity.StartOwnerID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.EndOwnerID))
            {
                sqlWhere.Add(" ISL.OWNER_ID   <= '" + replenishmentGenParamsEntity.EndOwnerID + "'");
            }
            if (!(string.IsNullOrEmpty(replenishmentGenParamsEntity.StartSkuID) || string.IsNullOrEmpty(replenishmentGenParamsEntity.EndSkuID)))
            {
                sqlWhere.Add(" ISL.SKU_ID  BETWEEN '" + replenishmentGenParamsEntity.StartSkuID + "' AND '" + replenishmentGenParamsEntity.EndSkuID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.StartSkuID))
            {
                sqlWhere.Add(" ISL.SKU_ID  >= '" + replenishmentGenParamsEntity.StartSkuID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.EndSkuID))
            {
                sqlWhere.Add(" ISL.SKU_ID  <= '" + replenishmentGenParamsEntity.EndSkuID + "'");
            }
            if (!(string.IsNullOrEmpty(replenishmentGenParamsEntity.StartZoneID) || string.IsNullOrEmpty(replenishmentGenParamsEntity.EndZoneID)))
            {
                sqlWhere.Add(" ISL.ZONE_ID  BETWEEN '" + replenishmentGenParamsEntity.StartZoneID + "' AND '" + replenishmentGenParamsEntity.EndZoneID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.StartZoneID))
            {
                sqlWhere.Add(" ISL.ZONE_ID  >= '" + replenishmentGenParamsEntity.StartZoneID + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.EndZoneID))
            {
                sqlWhere.Add(" ISL.ZONE_ID  <= '" + replenishmentGenParamsEntity.EndZoneID + "'");
            }
            if (!(string.IsNullOrEmpty(replenishmentGenParamsEntity.StartPriority) || string.IsNullOrEmpty(replenishmentGenParamsEntity.EndPriority)))
            {
                sqlWhere.Add(" ISL.PRIORITY  BETWEEN '" + replenishmentGenParamsEntity.StartPriority + "' AND '" + replenishmentGenParamsEntity.EndPriority + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.StartPriority))
            {
                sqlWhere.Add(" ISL.PRIORITY  >= '" + replenishmentGenParamsEntity.StartPriority + "'");
            }
            else if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.EndPriority))
            {
                sqlWhere.Add(" ISL.PRIORITY  <= '" + replenishmentGenParamsEntity.EndPriority + "'");
            }
            if (!string.IsNullOrEmpty(replenishmentGenParamsEntity.ReplenishmentUom))
            {
                sqlWhere.Add(" ISL.REPLENISHMENT_UOM  = '" + replenishmentGenParamsEntity.ReplenishmentUom + "'");
            }
            sqlWhere.Add(" (ISL.LOCATION_TYPE  = 'Case' OR  ISL.LOCATION_TYPE  = 'Piece')");
            List<InvSkuLocInfo> invSkuLocInfoList = InvSkuLoc.GetInvSkuLocBySqlWhere(sqlWhere, replenishmentGenParamsEntity.ReplenishmentTaskNumber, whLoginID, this.pubParasInfo);
            if ((invSkuLocInfoList != null) && (invSkuLocInfoList.Count > 0))
            {
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    try
                    {
                        Dictionary<string, SkuInfo> skuObjects = new Dictionary<string, SkuInfo>();
                        Dictionary<string, RotationRuleInfo> rotationRuleObjects = new Dictionary<string, RotationRuleInfo>();
                        Dictionary<string, PackInfo> packInfoObjects = new Dictionary<string, PackInfo>();
                        OutboundMgtBLL outboundMgtBLL = new OutboundMgtBLL(this.pubParasInfo);
                        foreach (InvSkuLocInfo info in invSkuLocInfoList)
                        {
                            if (string.IsNullOrEmpty(info.ReplenishmentUom))
                            {
                                goto Label_0F07;
                            }
                            SkuInfo skuinfo = null;
                            if (!skuObjects.ContainsKey(info.OwnerID + info.SkuID))
                            {
                                skuinfo = skuDA.GetSkuByID(dataBase, tran, info.OwnerID, info.SkuID, whLoginID);
                                skuObjects.Add(info.OwnerID + info.SkuID, skuinfo);
                            }
                            else
                            {
                                skuObjects.TryGetValue(info.OwnerID + info.SkuID, out skuinfo);
                            }
                            PackInfo packInfo = null;
                            if (!packInfoObjects.ContainsKey(skuinfo.PackID))
                            {
                                packInfo = packDA.GetPackByID(dataBase, tran, skuinfo.PackID, whLoginID);
                                packInfoObjects.Add(skuinfo.PackID, packInfo);
                            }
                            else
                            {
                                packInfoObjects.TryGetValue(skuinfo.PackID, out packInfo);
                            }
                            decimal UomQty = 0M;
                            string CS40004 = info.ReplenishmentUom;
                            if (CS40004 != null)
                            {
                                if (!(CS40004 == "10"))
                                {
                                    if (CS40004 == "20")
                                    {
                                        goto Label_05A9;
                                    }
                                    if (CS40004 == "30")
                                    {
                                        goto Label_05D8;
                                    }
                                    if (CS40004 == "40")
                                    {
                                        goto Label_0607;
                                    }
                                    if (CS40004 == "50")
                                    {
                                        goto Label_0633;
                                    }
                                    if (CS40004 == "60")
                                    {
                                        goto Label_065F;
                                    }
                                }
                                else if (packInfo.IsReplenishUom4 == "Y")
                                {
                                    UomQty = Convert.ToDecimal(packInfo.Pallet);
                                }
                            }
                            goto Label_068B;
                        Label_05A9:
                            if (packInfo.IsReplenishUom3 == "Y")
                            {
                                UomQty = Convert.ToDecimal(packInfo.CaseCnt);
                            }
                            goto Label_068B;
                        Label_05D8:
                            if (packInfo.IsReplenishUom2 == "Y")
                            {
                                UomQty = Convert.ToDecimal(packInfo.InnerPack);
                            }
                            goto Label_068B;
                        Label_0607:
                            if (packInfo.IsReplenishUom8 == "Y")
                            {
                                UomQty = Convert.ToDecimal(packInfo.OtherUnit1);
                            }
                            goto Label_068B;
                        Label_0633:
                            if (packInfo.IsReplenishUom9 == "Y")
                            {
                                UomQty = Convert.ToDecimal(packInfo.OtherUnit2);
                            }
                            goto Label_068B;
                        Label_065F:
                            if (packInfo.IsReplenishUom1 == "Y")
                            {
                                UomQty = Convert.ToDecimal(packInfo.Qty);
                            }
                        Label_068B:
                            if (UomQty == 0M)
                            {
                                continue;
                            }
                            decimal replenishmentQty = UomQty;
                            bool isContiune = false;
                            if (info.QtyLocationMinimum <= 0M)
                            {
                                continue;
                            }
                            for (int i = 0; i > -1; i++)
                            {
                                if ((info.Qty + replenishmentQty) < info.QtyLocationMinimum)
                                {
                                    replenishmentQty += UomQty;
                                }
                                else
                                {
                                    if (info.QtyLocationLimit > 0M)
                                    {
                                        if ((info.Qty + replenishmentQty) > info.QtyLocationLimit)
                                        {
                                            isContiune = true;
                                        }
                                    }
                                    else
                                    {
                                        isContiune = true;
                                    }
                                    break;
                                }
                            }
                            if (isContiune)
                            {
                                continue;
                            }
                            RotationRuleInfo rotationRuleInfo = null;
                            if (!rotationRuleObjects.ContainsKey(skuinfo.RotationRuleID))
                            {
                                rotationRuleInfo = rotataionRuleDA.GetRotationRuleByID(dataBase, tran, skuinfo.RotationRuleID, this.dataBaseID[0]);
                                if (rotationRuleInfo != null)
                                {
                                    rotationRuleInfo.RotationRuleDetailInfo = rotataionRuleDetailDA.GetRotationRuleDetailListByID(dataBase, tran, rotationRuleInfo.RotationRuleID, this.dataBaseID[0]);
                                    rotationRuleObjects.Add(rotationRuleInfo.RotationRuleID, rotationRuleInfo);
                                }
                            }
                            else
                            {
                                rotationRuleObjects.TryGetValue(skuinfo.RotationRuleID, out rotationRuleInfo);
                            }
                            string orderBy = outboundMgtBLL.OrganizeRotationRule(rotationRuleInfo);
                            List<string> LocUsage = new List<string>();
                            if (info.AllowReplenishFromBulk == 1)
                            {
                                LocUsage.Add("Storage");
                            }
                            if (info.AllowReplenishFromCasepick == 1)
                            {
                                LocUsage.Add("Case");
                            }
                            if (info.AllowReplenishFromPiecepick == 1)
                            {
                                LocUsage.Add("Piece");
                            }
                            if (LocUsage.Count == 0)
                            {
                                break;
                            }
                            List<InventoryInfo> inventoryInfos = inventoryMgtDA.GetInventoryInfoByReplenishment(dataBase, tran, info.OwnerID, info.SkuID, LocUsage, "OK", orderBy, this.dataBaseID[0]);
                            decimal invQty = 0M;
                            foreach (InventoryInfo invInfo in inventoryInfos)
                            {
                                if (invInfo.Qty > 0M)
                                {
                                    ReplenishmentInfo replenishmentInfo;
                                    invQty = (((((invInfo.Qty - invInfo.AllocatedQty) - invInfo.PickedQty) - invInfo.ArchiveQty) - invInfo.ExpectedQty) - invInfo.PickinprocessQty) - invInfo.PendingMoveIn;
                                    if (invQty >= replenishmentQty)
                                    {
                                        invQty = replenishmentQty;
                                        replenishmentQty = 0M;
                                    }
                                    else if (invQty > 0M)
                                    {
                                        replenishmentQty -= invQty;
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                    InvLotInfo invLotinfo = new InvLotInfo {
                                        Lot = invInfo.Lot,
                                        UpdatedBy = this.pubParasInfo.UserID,
                                        AllocatedQty = invQty,
                                        AllocatedGrossWgt = 0.0,
                                        AllocatedNetWgt = 0.0
                                    };
                                    invlotDA.UpdateInvLot(dataBase, tran, invLotinfo, InvLotUpdateType.AllocatedQtyWgtOnly, whLoginID);
                                    InvLotLocLpnInfo invLotLocLpnInfo = new InvLotLocLpnInfo {
                                        Lot = invInfo.Lot,
                                        Loc = invInfo.Loc,
                                        LpnID = invInfo.LpnID,
                                        AllocatedQty = invQty,
                                        UpdatedBy = this.pubParasInfo.UserID
                                    };
                                    invLotLocLpnDA.UpdateInvLotLocLpn(dataBase, tran, invLotLocLpnInfo, InvLotLocLpnUpdateType.AllocatedQty, whLoginID);
                                    InvSkuLocInfo invskulocInfo = new InvSkuLocInfo {
                                        Loc = invInfo.Loc,
                                        SkuID = invInfo.SkuID,
                                        OwnerID = invInfo.OwnerID,
                                        UpdatedBy = this.pubParasInfo.UserID,
                                        AllocatedQty = invQty
                                    };
                                    InvSkuLoc.UpdateInvSkuLoc(dataBase, tran, invskulocInfo, InvSkuLocUpdateType.AllocatedQty, whLoginID);
                                    replenishmentInfo = new ReplenishmentInfo();
                                    replenishmentInfo.ReplenishmentID = commonMethod.GenNextNumStr("REPLENISHMENT", whLoginID);
                                    replenishmentInfo.ReplenishmentGroup = replenishmentInfo.ReplenishmentID;
                                    replenishmentInfo.OwnerID = invInfo.OwnerID;
                                    replenishmentInfo.SkuID = invInfo.SkuID;
                                    replenishmentInfo.FromLoc = invInfo.Loc;
                                    replenishmentInfo.ToLoc = info.Loc;
                                    replenishmentInfo.Lot = invInfo.Lot;
                                    replenishmentInfo.LpnID = invInfo.LpnID;
                                    replenishmentInfo.Qty = invQty;
                                    replenishmentInfo.QtyMoved = invQty;
                                    replenishmentInfo.QtyInpickLoc = 0M;
                                    replenishmentInfo.Priority = "5";
                                    replenishmentInfo.UomID = packInfo.PackUom1;
                                    replenishmentInfo.PackID = packInfo.PackID;
                                    replenishmentInfo.IdMoved = "";
                                    replenishmentInfo.LotMoved = "";
                                    replenishmentInfo.LocMovedFrom = "";
                                    replenishmentInfo.Reason = "";
                                    replenishmentInfo.ToLpnID = invInfo.LpnID;
                                    replenishmentInfo.FinalToLoc = "";
                                    replenishmentInfo.CreatedBy = this.pubParasInfo.UserID;
                                    replenishmentInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    replenishmentDA.InsertReplenishment(dataBase, tran, replenishmentInfo, whLoginID);
                                    TaskDetailInfo taskDetailEntity = new TaskDetailInfo {
                                        TaskDetailID = commonMethod.GenNextNumStr("TASK_DETAIL", whLoginID),
                                        TaskType = TaskType.RP,
                                        OwnerID = invInfo.OwnerID,
                                        SkuID = invInfo.SkuID,
                                        Lot = invInfo.Lot,
                                        UomID = packInfo.PackUom1,
                                        UomQty = UomQty,
                                        Qty = invQty,
                                        FromLoc = invInfo.Loc,
                                        LogicalFromLoc = invInfo.Loc,
                                        FromLpnID = invInfo.LpnID,
                                        ToLoc = info.Loc,
                                        LogicalToLoc = info.Loc,
                                        ToLpnID = "",
                                        CaseID = "",
                                        PickMethod = "",
                                        Status = TaskStatus.Pending,
                                        StatusMsg = "",
                                        Priority = TaskPriority.five,
                                        SourcePriority = "",
                                        HoldID = "",
                                        UserID = "",
                                        UserPosition = "",
                                        UserIdOverride = "",
                                        StartTime = DateTime.Now,
                                        EndTime = DateTime.Now,
                                        SourceType = "",
                                        DocID = replenishmentInfo.ReplenishmentID,
                                        DocLineID = 0,
                                        PickDetailID = "",
                                        OrderID = "",
                                        OrderLineID = 0,
                                        ListID = "",
                                        WaveID = "",
                                        ReasonID = "",
                                        Message01 = "",
                                        Message02 = "",
                                        Message03 = "",
                                        FinalToLoc = "",
                                        FromTransID = "",
                                        ToTransID = "",
                                        CreatedBy = this.pubParasInfo.UserID,
                                        UpdatedBy = this.pubParasInfo.UserID
                                    };
                                    taskDetailList.Add(taskDetailEntity);
                                    taskDetailDA.InsertTaskDetail(dataBase, tran, taskDetailEntity, whLoginID);
                                    if (replenishmentQty == 0M)
                                    {
                                        break;
                                    }
                                }
                            }
                        Label_0F07:;
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    return taskDetailList;
                }
                finally
                {
                    conn.Close();
                }
            }
            return taskDetailList;
        }

        public DataSet GetAllReplenishment()
        {
            return replenishmentDA.GetAllReplenishment(this.dataBaseID[0], this.pubParasInfo);
        }

        private List<InvLotLocLpnInfo> GetInvSkuLocInfoByInvSkuLocInfo(InvSkuLocInfo invSkuLocInfo, string LocType)
        {
            List<InvLotLocLpnInfo> invLotLocLpnInfo = new List<InvLotLocLpnInfo>();
            string CS40001 = LocType;
            if (CS40001 != null)
            {
                if (!(CS40001 == "0"))
                {
                    if (CS40001 != "1")
                    {
                        if ((CS40001 == "2") && (invSkuLocInfo.AllowReplenishFromPiecepick == 1))
                        {
                            invLotLocLpnInfo = invLotLocLpnDA.GetInvLotLocLpnByLocType(invSkuLocInfo.OwnerID, invSkuLocInfo.SkuID, invSkuLocInfo.Loc, "PIECE", this.dataBaseID[0]);
                        }
                        return invLotLocLpnInfo;
                    }
                }
                else
                {
                    if (invSkuLocInfo.AllowReplenishFromBulk == 1)
                    {
                        invLotLocLpnInfo = invLotLocLpnDA.GetInvLotLocLpnByLocType(invSkuLocInfo.OwnerID, invSkuLocInfo.SkuID, invSkuLocInfo.Loc, "STORAGE", this.dataBaseID[0]);
                    }
                    return invLotLocLpnInfo;
                }
                if (invSkuLocInfo.AllowReplenishFromCasepick == 1)
                {
                    invLotLocLpnInfo = invLotLocLpnDA.GetInvLotLocLpnByLocType(invSkuLocInfo.OwnerID, invSkuLocInfo.SkuID, invSkuLocInfo.Loc, "CASE", this.dataBaseID[0]);
                }
            }
            return invLotLocLpnInfo;
        }

        public ReplenishmentInfo GetReplenishmentByID(string replenishmentID)
        {
            return replenishmentDA.GetReplenishmentByID(replenishmentID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetReplenishmentByQueryList(ReplenishmentQueryEntity replenishmentQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(replenishmentQueryEntity.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + replenishmentQueryEntity.SkuID);
            }
            if (!string.IsNullOrEmpty(replenishmentQueryEntity.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + replenishmentQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(replenishmentQueryEntity.Lot))
            {
                sqlWhere.Add(" LOT " + replenishmentQueryEntity.Lot);
            }
            if (!string.IsNullOrEmpty(replenishmentQueryEntity.FromLoc))
            {
                sqlWhere.Add(" FROM_LOC " + replenishmentQueryEntity.FromLoc);
            }
            if (!string.IsNullOrEmpty(replenishmentQueryEntity.ToLoc))
            {
                sqlWhere.Add(" TO_LOC " + replenishmentQueryEntity.ToLoc);
            }
            return replenishmentDA.GetReplenishmentByQueryList(sqlWhere, replenishmentQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertReplenishment(ReplenishmentInfo replenishmentInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strReplenishmentID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(replenishmentInfo.ReplenishmentID))
                {
                    replenishmentInfo.ReplenishmentID = commonMethod.GenNextNumStr(dataBase, tran, "ACCESSORIAL", whLoginID);
                }
                replenishmentInfo.UpdatedBy = replenishmentInfo.CreatedBy;
                replenishmentInfo.CreatedDate = DateTime.Now;
                replenishmentInfo.UpdatedDate = DateTime.Now;
                if (replenishmentDA.InsertReplenishment(dataBase, tran, replenishmentInfo, whLoginID) > 0)
                {
                    strReplenishmentID = replenishmentInfo.ReplenishmentID;
                    new AuditDataBLL().InsertAuditData<ReplenishmentInfo>(dataBase, tran, null, replenishmentInfo, AuditAction.Insert, this.pubParasInfo.UserID, "REPLENISHMENT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strReplenishmentID;
        }

        private void Replenishment(InvSkuLocInfo invSkuLocInfo, SkuInfo skuinfo, decimal UomQty)
        {
            decimal ReplenishQty = 0M;
            if (invSkuLocInfo.ExpectedQty > 0M)
            {
                ReplenishQty = Math.Ceiling((decimal) (invSkuLocInfo.ExpectedQty / UomQty)) * UomQty;
            }
            else if (invSkuLocInfo.Qty < invSkuLocInfo.QtyLocationMinimum)
            {
                ReplenishQty = Math.Ceiling((decimal) ((invSkuLocInfo.QtyLocationLimit - invSkuLocInfo.Qty) / UomQty)) * UomQty;
            }
            if (ReplenishQty < invSkuLocInfo.QtyLocationMinimum)
            {
                ReplenishQty = Math.Ceiling((decimal) (invSkuLocInfo.QtyLocationMinimum / UomQty)) * UomQty;
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                decimal qty = ReplenishQty;
                string strGroup = commonMethod.GenNextNumStr(dataBase, tran, "REPLENISHMENT", this.dataBaseID[0]);
                for (int i = 0; i < 3; i++)
                {
                    List<InvLotLocLpnInfo> invLotLocLpnInfo = this.GetInvSkuLocInfoByInvSkuLocInfo(invSkuLocInfo, i.ToString());
                    if ((invLotLocLpnInfo != null) && (invLotLocLpnInfo.Count > 0))
                    {
                        foreach (InvLotLocLpnInfo info in invLotLocLpnInfo)
                        {
                            if (((info.Qty - info.PickedQty) - info.PickinprocessQty) > 0M)
                            {
                                ReplenishmentInfo replenishmentInfo;
                                if (qty > ((info.Qty - info.PickedQty) - info.PickinprocessQty))
                                {
                                    qty -= info.Qty;
                                }
                                else
                                {
                                    qty = 0M;
                                }
                                replenishmentInfo = new ReplenishmentInfo();
                                replenishmentInfo.ReplenishmentID = commonMethod.GenNextNumStr(dataBase, tran, "REPLENISHMENT", this.dataBaseID[0]);
                                replenishmentInfo.ReplenishmentGroup = strGroup;
                                replenishmentInfo.OwnerID = invSkuLocInfo.OwnerID;
                                replenishmentInfo.SkuID = invSkuLocInfo.SkuID;
                                replenishmentInfo.FromLoc = info.Loc;
                                replenishmentInfo.ToLoc = invSkuLocInfo.Loc;
                                replenishmentInfo.Lot = info.Lot;
                                replenishmentInfo.LpnID = info.LpnID;
                                replenishmentInfo.Qty = ReplenishQty - qty;
                                replenishmentInfo.QtyInpickLoc = 0M;
                                replenishmentInfo.QtyMoved = 0M;
                                replenishmentInfo.Priority = TaskPriority.five;
                                replenishmentInfo.UomID = invSkuLocInfo.ReplenishmentUom;
                                replenishmentInfo.PackID = skuinfo.PackID;
                                replenishmentInfo.IdMoved = "";
                                replenishmentInfo.LotMoved = "";
                                replenishmentInfo.LocMovedFrom = "";
                                replenishmentInfo.Reason = "";
                                replenishmentInfo.ToLpnID = "";
                                replenishmentInfo.FinalToLoc = "";
                                replenishmentInfo.CreatedBy = this.pubParasInfo.UserID;
                                replenishmentInfo.UpdatedBy = replenishmentInfo.CreatedBy;
                                replenishmentDA.InsertReplenishment(dataBase, tran, replenishmentInfo, this.dataBaseID[0]);
                                if (ReplenishQty == 0M)
                                {
                                    InvSkuLoc.UpdateInvSkuLoc(dataBase, tran, invSkuLocInfo, this.dataBaseID[0]);
                                    break;
                                }
                            }
                        }
                    }
                    if (ReplenishQty == 0M)
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
        }

        public string ReplenishmentConfirm(List<string> Ids)
        {
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                InventoryMgtBLL inventoryMgt = new InventoryMgtBLL(this.pubParasInfo);
                for (int i = 0; Ids.Count > i; i++)
                {
                    ReplenishmentInfo replenishmentInfo = replenishmentDA.GetReplenishmentByID(dataBase, tran, Ids[i], whLoginID, this.pubParasInfo);
                    if (replenishmentInfo != null)
                    {
                        InvMovesInfo invMovesInfo = new InvMovesInfo {
                            FromLoc = replenishmentInfo.FromLoc,
                            FromLot = replenishmentInfo.Lot,
                            FromLpn = replenishmentInfo.LpnID,
                            FromMoveQty = replenishmentInfo.Qty,
                            FromOwnerID = replenishmentInfo.OwnerID,
                            FromPackID = replenishmentInfo.PackID,
                            FromSkuID = replenishmentInfo.SkuID,
                            FromUomID = replenishmentInfo.UomID,
                            MoveQty = replenishmentInfo.Qty,
                            PackID = replenishmentInfo.PackID,
                            ToLoc = replenishmentInfo.ToLoc,
                            ToLot = replenishmentInfo.Lot,
                            ToLpn = replenishmentInfo.LpnID,
                            ToOwnerID = replenishmentInfo.OwnerID,
                            ToSkuID = replenishmentInfo.SkuID,
                            UomID = replenishmentInfo.UomID,
                            UpdatedBy = this.pubParasInfo.UserID
                        };
                        inventoryMgt.UpdateInvLotLocLpn4Moves(dataBase, tran, invMovesInfo, InvMoveType.Replenishment, whLoginID);
                        inventoryMgt.UpdateInvSkuLoc4Moves(dataBase, tran, invMovesInfo, InvMoveType.Replenishment, whLoginID);
                        inventoryMgt.UpdateInvLot4Moves(dataBase, tran, invMovesInfo, InvMoveType.Replenishment, whLoginID);
                        inventoryMgt.UpdateLpn4Moves(dataBase, tran, invMovesInfo, whLoginID);
                        string transID = inventoryMgt.InsertInvMovesTrans(dataBase, tran, invMovesInfo, TransSourceType.TaskAdd, whLoginID);
                        TaskDetailInfo taskDetailInfo = taskDetailDA.GetTaskDetailInfoByDocAndLineID(dataBase, tran, replenishmentInfo.ReplenishmentID, "0", TaskType.RP, whLoginID, this.pubParasInfo);
                        if (taskDetailInfo != null)
                        {
                            taskDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                            taskDetailInfo.UpdatedDate = DateTime.Now;
                            taskDetailInfo.FinalToLoc = replenishmentInfo.ToLoc;
                            taskDetailInfo.FromTransID = "";
                            taskDetailInfo.ToTransID = transID;
                            taskDetailInfo.Status = TaskStatus.Completed;
                            taskDetailDA.UpdateTaskDetail(dataBase, tran, taskDetailInfo, whLoginID);
                        }
                        temp = temp + replenishmentInfo.ReplenishmentID + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string UpdateReplenishment(ReplenishmentInfo replenishmentInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strReplenishmentID = string.Empty;
            try
            {
                ReplenishmentInfo oldReplenishmentInfo = replenishmentDA.GetReplenishmentByID(dataBase, tran, replenishmentInfo.ReplenishmentID, this.dataBaseID[0], this.pubParasInfo);
                if (replenishmentDA.UpdateReplenishment(dataBase, tran, replenishmentInfo, whLoginID) > 0)
                {
                    strReplenishmentID = replenishmentInfo.ReplenishmentID;
                    new AuditDataBLL().InsertAuditData<ReplenishmentInfo>(dataBase, tran, oldReplenishmentInfo, replenishmentInfo, AuditAction.Update, this.pubParasInfo.UserID, "REPLENISHMENT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strReplenishmentID;
        }
    }
}

